package cn.hubujyjs.lp.service.impl;

import cn.hubujyjs.lp.bo.AdminBo;
import cn.hubujyjs.lp.bo.AdminLoginBo;
import cn.hubujyjs.lp.bo.UserBo;
import cn.hubujyjs.lp.config.AdminHolder;
import cn.hubujyjs.lp.config.UserHolder;
import cn.hubujyjs.lp.domain.User;
import cn.hubujyjs.lp.utils.R;
import cn.hubujyjs.lp.vo.AdminVo;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hubujyjs.lp.domain.Admin;
import cn.hubujyjs.lp.service.AdminService;
import cn.hubujyjs.lp.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static cn.hubujyjs.lp.constant.RedisConst.*;

/**
* @author admin
* @description 针对表【admin(管理员表)】的数据库操作Service实现
* @createDate 2023-06-01 16:18:43
*/
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
    implements AdminService{
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 管理员登录
     * @param adminLoginBo 管理员登录实体
     * @return token
     */
    @Override
    public R<String> login(AdminLoginBo adminLoginBo) {
        String username = adminLoginBo.getUsername();
        String password = adminLoginBo.getPassword();
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)){
            return R.fail("用户名或密码不能为空");
        }
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getUsername,username);
        Admin one = this.getOne(wrapper);
        if (one==null){
            return R.fail("账号不存在");
        }
        boolean b = DigestUtil.bcryptCheck(password, one.getPassword());
        if (!b){
            return R.fail("账号或密码错误");
        }
        //以admin开头的token表示管理员
        String token = "admin "+UUID.randomUUID().toString(true);
        AdminBo adminBo = BeanUtil.copyProperties(one, AdminBo.class);
        Map<String, Object> map = BeanUtil.beanToMap(adminBo);
        map.replaceAll((k, v) -> String.valueOf(v));

        String tokenKey = PL_ADMIN_KEY_ + token;
        stringRedisTemplate.opsForHash().putAll(tokenKey, map);
        //设置token有效期
        stringRedisTemplate.expire(tokenKey, LOGIN_USER_TTL, TimeUnit.DAYS);

        return R.ok(token);
    }
    /**
     * 查询用户信息
     * @return 用户实体
     */
    @Override
    public R<AdminVo> info() {
        Integer id = AdminHolder.getUser().getId();
        Admin byId = this.getById(id);
        AdminVo build = AdminVo.builder()
                .id(byId.getId())
                .picture(byId.getPicture())
                .username(byId.getUsername())
                .build();
        return R.ok(build);
    }
    /**
     * 退出登录
     * @param request Httprequest请求
     * @return null
     */
    @Override
    public R<Object> logout(HttpServletRequest request) {
        String header = request.getHeader("X-Token");
        String token=PL_ADMIN_KEY_ + header;
        stringRedisTemplate.delete(token);
        return R.ok();
    }
}




